package com.quanyue.music.file.mapper;

import com.quanyue.music.domain.constant.FMChannel;
import com.quanyue.music.domain.entity.LikeDetail;
import com.quanyue.music.file.type.FMDetailLikeRequest;
import com.quanyue.music.file.type.FMDetailLikeResult;
import com.quanyue.music.file.type.FMHotResult;
import com.quanyue.music.file.type.ZoneCount;
import com.shzisg.mybatis.mapper.auto.AutoMapper;
import com.shzisg.mybatis.mapper.page.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface StatisticsMapper extends AutoMapper <LikeDetail> {

    @Select("select pl.id as fm_id, f.key_id, f.title as music_title, f.singer_name, pl.start_time, pl.end_time "
            + "from qy_play_list pl "
            + "left join qy_fm f on f.id = pl.fm_music "
            + "where DATE_FORMAT(pl.start_time, '%Y-%m-%d') = #{date} "
            + "and pl.channel_num = #{channelNum} ")
    Page <FMDetailLikeResult> findPlayMusic(@Param("date") LocalDate date,
                                            @Param("channelNum") FMChannel channelNum,
                                            @Param("request") FMDetailLikeRequest request);

    @Select("select pl.id, pl.start_time, pl.end_time, IFNULL(count(pl.id),0) as total_count "
            + "from qy_play_list pl "
            + "left join qy_listen_record r on r.fm_id = pl.id "
            + "where DATE_FORMAT(pl.start_time, '%Y-%m-%d') = #{date} "
            + "and pl.channel_num = #{channelNum} "
            + "group by pl.id")
    List <FMHotResult> findPlayList(@Param("date") LocalDate date,
                                    @Param("channelNum") FMChannel channelNum);

    @Select("select pl.id as fm_id, f.key_id, f.title as music_title, f.singer_name, pl.start_time, pl.end_time "
            + "from qy_play_list pl "
            + "left join qy_fm f on f.id = pl.fm_music "
            + "where DATE_FORMAT(pl.start_time, '%Y-%m-%d') = #{date} "
            + "and pl.channel_num = #{channelNum} "
            + "order by pl.start_time")
    List <FMDetailLikeResult> findPlayMusicExcel(@Param("date") LocalDate date,
                                                 @Param("channelNum") FMChannel channelNum);

    @Select("select f.key_id from qy_play_list pl "
            + "left join qy_fm f on f.id = pl.fm_advertise "
            + "where pl.id = #{fmId}")
    String findAdvertiseKey(@Param("fmId") String fmId);

    @Select("select d.zone as zone_type, IFNULL(d.count,0) as count from qy_like_detail d where d.fm_id = #{fmId}")
    List <ZoneCount> findLikeCount(@Param("fmId") String fmId);

    @Select("select r.zone_type, count(1) as count from qy_listen_record r where r.fm_id = #{fmId} group by r.zone_type")
    List <ZoneCount> findListenCount(@Param("fmId") String fmId);
}
